package com.wz.project.stock.stock.controller;

import com.wz.common.utils.poi.ExcelUtil;
import com.wz.device.entity.BasCargoSpace;
import com.wz.framework.aspectj.lang.annotation.Log;
import com.wz.framework.aspectj.lang.enums.BusinessType;
import com.wz.framework.web.controller.BaseController;
import com.wz.framework.web.domain.AjaxResult;
import com.wz.framework.web.page.TableDataInfo;
import com.wz.project.bas.basJobId.domain.BasJobId;
import com.wz.project.bas.basJobId.service.IBasJobIdService;
import com.wz.project.stock.domain.BasStock;
import com.wz.project.stock.stock.service.IBasStockService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 库存Controller
 * 
 * @author wz
 * @date 2021-11-12
 */
@Controller
@RequestMapping("/stock/stockCheck")
public class BasStockController extends BaseController
{
    private String prefix = "stock/stock";

    @Autowired
    private IBasStockService basStockService;
    @Autowired
    private IBasJobIdService basJobIdService;
    @RequiresPermissions("stock:stock:view")
    @GetMapping()
    public String stock()
    {
        return prefix + "/stock";
    }

    /**
     * 查询库存列表
     */
//    @RequiresPermissions("stock:stock:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BasStock basStock)
    {
        startPage();

        List<BasStock> list = basStockService.selectBasStockList(basStock);
        return getDataTable(list);
    }

    @PostMapping("/list1")
    @ResponseBody
    public TableDataInfo list1(BasStock basStock)
    {
        startPage();
        List<BasStock> list = basStockService.selectBasStockList2(basStock);
        return getDataTable(list);
    }

    /**
     * 导出库存列表
     */
    @RequiresPermissions("stock:stock:export")
    @Log(title = "库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BasStock basStock)
    {
        List<BasStock> list = basStockService.selectBasStockList(basStock);
        ExcelUtil<BasStock> util = new ExcelUtil<BasStock>(BasStock.class);
        return util.exportExcel(list, "stock");
    }

    /**
     * 新增库存
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存库存
     */
    @RequiresPermissions("stock:stock:add")
    @Log(title = "库存新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BasStock basStock)
    {
        String kc = basJobIdService.getjobId("KC");
        basStock.setStockId(kc);
        basStock.setBackDate(new SimpleDateFormat("yyyy-mm-dd hh:MM:ss").format(new Date()));
        basStock.setInDate(new SimpleDateFormat("yyyy-mm-dd hh:MM:ss").format(new Date()));
        return toAjax(basStockService.insertBasStock(basStock));
    }

    /**
     * 修改库存
     */
    @GetMapping("/edit{stockId}")
    public String edit(@PathVariable("stockId") String stockId, ModelMap mmap)
    {
        BasStock basStock = basStockService.selectBasStockById(stockId);
        mmap.put("basStock", basStock);
        return prefix + "/edit";
    }

    /**
     * 修改保存库存
     */

//    @RequiresPermissions("stock:stock:updateStock")
    @Log(title = "库存修改", businessType = BusinessType.UPDATE)
    @RequestMapping("/updateStock")
    @ResponseBody
    public AjaxResult updateStock(@Validated BasStock basStock){
        return toAjax(basStockService.updateBasStock(basStock));
    }
    /**
     * 删除库存
     */
    @RequiresPermissions("stock:stock:remove")
    @Log(title = "库存删除", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(basStockService.deleteBasStockByIds(ids));
    }

    /**
     * 查找货位
     */
    @RequiresPermissions("stock:stock:cargoList")
    @RequestMapping("/cargoList")
    @ResponseBody
    public TableDataInfo listCargo(BasCargoSpace basCargoSpace)
    {

        startPage();
        List<BasCargoSpace> list = basStockService.findCargoList(basCargoSpace);
        return getDataTable(list);
    }

    @RequestMapping("/selectCargo")
    public String listCargo()
    {
        return "/stock/stock/selectCargo";
    }
}
